package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.xspace.android.xafcommon.been.MAreaBean;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.db.SDBaseSql;

import java.util.ArrayList;
import java.util.List;



public class MAreaDB extends BaseSql {
	private String id = "aid";
	private String name = "name";
	private String parentcode = "parentcode";
	private String isleaf = "isLeaf";
	private String level = "level";
	private String TableName = "marea";
	private MyDBHelper helpDb;
	SQLiteDatabase sqldb;

	public MAreaDB(MyDBHelper db) {
		super(db);
		helpDb = db;
	}

	public MAreaDB() {

	}
	public void closeDb() {
		//helpDb.close();

	}
	@Override
	public void create(SQLiteDatabase db) {

		db.execSQL("CREATE TABLE " + TableName
				+ " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," + id + " text,"
				+ name + " text," + parentcode + " text," + level + " text,"
				+ isleaf + " text);");
	}

	public void dropTable(SQLiteDatabase db) {

		db.execSQL("DROP TABLE IF EXISTS " + TableName);
	}

	public void openDb() {
		helpDb.open();
		sqldb = helpDb.mDatabase;

	}

	@Override
	public void upgrade(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	private void insertOne(MAreaBean ab) {
		ContentValues cv = new ContentValues();
		cv.put(id, ab.id);
		cv.put(name, ab.name);
		cv.put(parentcode, ab.parentcode);
		cv.put(level, ab.level);
		cv.put(isleaf, ab.isleaf);
		sqldb.insert(TableName, null, cv);
	}

	public void MoreInsert(List<MAreaBean> mareaList) {
		if (mareaList == null || mareaList.size() == 0)
			return;
		openDb();
		if (isExistData())
			return;
		sqldb.beginTransaction();
		Log.i("areadb", "MoreInsert mareaList:" + mareaList.size());
		for (int i = 0; i < mareaList.size(); i++) {
			MAreaBean area = mareaList.get(i);
			insertOne(area);
		}
		sqldb.setTransactionSuccessful();
		sqldb.endTransaction();
	}

	public boolean isExistData() {
		boolean exist = false;
		String[] columns = { name, id, parentcode, level, isleaf };
		Cursor cur = sqldb.query(TableName, columns, null, null,
				null, null, null);
		if (cur != null) {
			if (cur.getCount() > 0) {
				exist = true;
			}
			cur.close();
		}
		return exist;
	}

	public List<MAreaBean> queryAreasByPid(String pid) {
		List<MAreaBean> mareaList = new ArrayList<MAreaBean>();
		String[] columns = { name, id, parentcode, level, isleaf };
		Cursor cur = sqldb.query(TableName, columns, parentcode
				+ "=?", new String[] { pid }, null, null, null);
		if (cur != null) {
			if (cur.getCount() > 0) {
				while (cur.moveToNext()) {
					String name = cur.getString(0);
					String id = cur.getString(1);
					String parentcode = cur.getString(2);
					String le = cur.getString(3);
					String isleaf = cur.getString(4);
					MAreaBean ab = new MAreaBean(id, name, parentcode, isleaf,
							le);
					mareaList.add(ab);
				}
			}
			cur.close();
		}

		return mareaList;

	}
}
